/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;

/**
 *
 * @author Zhang Ying
 */
@Entity
public class IndivForecast implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String createdType; // indicate to be whether "Historical", "Industry" or "Static"(consts)
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> baseProjectionSelected = new ArrayList<IndexNValue>(); // 6
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> promotionList = new ArrayList<IndexNValue>(); // the size is the same as numOfMonth 6
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> humanAdjustList = new ArrayList<IndexNValue>(); // the size is the same as numOfMonth 6
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> finalValues = new ArrayList<IndexNValue>(); // the size is the same as numOfMonth 6
    
    // to indicate which product, 0- A, 1-B, 2-C, 3-D
    private int productIndex;
    @ManyToOne(targetEntity=Product.class, cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
    private Product product;
    
    @OneToOne(targetEntity = HistoricalInd.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private HistoricalInd historicalSF;
    
    @OneToOne(targetEntity = IndustryInd.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private IndustryInd industrySF;
    
    @OneToOne(targetEntity = StaticInd.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private StaticInd staticSF;
    
    @OrderBy("updateIndex")
    @OneToMany(targetEntity = UpdateIndiv.class, cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
    private Collection<UpdateIndiv> updateIndivs = new ArrayList<UpdateIndiv>();

    public IndivForecast() {
        // create 3 ind
        historicalSF = new HistoricalInd();
        industrySF = new IndustryInd();
        staticSF = new StaticInd();
        // init 3 lists
        for (int i = 0; i < 6; i++){
            IndexNValue temp1 = new IndexNValue(i, new Long(0));
            IndexNValue temp2 = new IndexNValue(i, new Long(0));
            IndexNValue temp3 = new IndexNValue(i, new Long(0));
            IndexNValue temp4 = new IndexNValue(i, new Long(0));
            promotionList.add(temp1);
            humanAdjustList.add(temp2);
            finalValues.add(temp3);
            baseProjectionSelected.add(temp4);
        }
    }

    public UpdateIndiv returnUpdateIndivs(int i){
        if (updateIndivs == null || updateIndivs.isEmpty()){
            return null;
        }
        List<UpdateIndiv> temp = (List<UpdateIndiv>) updateIndivs;
        return temp.get(i);
    }
    
    public Integer returnNumberOfUpdates(){
        return updateIndivs.size();
    }

    public int getProductIndex() {
        return productIndex;
    }

    public void setProductIndex(int productIndex) {
        this.productIndex = productIndex;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCreatedType() {
        return createdType;
    }

    public void setCreatedType(String createdType) {
        this.createdType = createdType;
    }

    public HistoricalInd getHistoricalSF() {
        return historicalSF;
    }

    public void setHistoricalSF(HistoricalInd historicalSF) {
        this.historicalSF = historicalSF;
    }

    public IndustryInd getIndustrySF() {
        return industrySF;
    }

    public void setIndustrySF(IndustryInd industrySF) {
        this.industrySF = industrySF;
    }

    public StaticInd getStaticSF() {
        return staticSF;
    }

    public void setStaticSF(StaticInd staticSF) {
        this.staticSF = staticSF;
    }

    public Collection<UpdateIndiv> getUpdateIndivs() {
        return updateIndivs;
    }

    public void setUpdateIndivs(Collection<UpdateIndiv> updateIndivs) {
        this.updateIndivs = updateIndivs;
    }

    public List<IndexNValue> getBaseProjectionSelected() {
        return baseProjectionSelected;
    }

    public void setBaseProjectionSelected(List<IndexNValue> baseProjectionSelected) {
        this.baseProjectionSelected = baseProjectionSelected;
    }

    public List<IndexNValue> getFinalValues() {
        return finalValues;
    }

    public void setFinalValues(List<IndexNValue> finalValues) {
        this.finalValues = finalValues;
    }

    public List<IndexNValue> getHumanAdjustList() {
        return humanAdjustList;
    }

    public void setHumanAdjustList(List<IndexNValue> humanAdjustList) {
        this.humanAdjustList = humanAdjustList;
    }

    public List<IndexNValue> getPromotionList() {
        return promotionList;
    }

    public void setPromotionList(List<IndexNValue> promotionList) {
        this.promotionList = promotionList;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof IndivForecast)) {
            return false;
        }
        IndivForecast other = (IndivForecast) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.IndivForecast[ id=" + id + " ]";
    }
}
